package com.gyg.service.impl;

import com.gyg.constant.CloudConstant;
import com.gyg.domain.CloudDocument;
import com.gyg.dao.CloudDocumentMapper;
import com.gyg.enums.DocumentTypeEnum;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.returns.RespTable;
import com.gyg.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.vo.PageVO;
import com.gyg.vo.param.*;

import com.gyg.vo.result.DocumentResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文档信息表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
public class CloudDocumentServiceImpl extends ServiceImpl<CloudDocumentMapper, CloudDocument> implements CloudDocumentService {

    @Override
    public RespTable<List<DocumentResult>> documentList(FileParam fileParam) {
        Integer count = baseMapper.documentCount(fileParam);

        if (Objects.isNull(count) || 0 == count) {
            return new RespTable<>(RespCode.OK, new ArrayList<>(), 0);
        }

        PageVO pageVO = new PageVO(fileParam.getPage(), fileParam.getLimit(), count);
        fileParam.setPage(pageVO.getBegin() - 1);
        List<DocumentResult> documentResultList = baseMapper.documentList(fileParam);

        return new RespTable<>(RespCode.OK, documentResultList, count);
    }

    @Override
    public RespEntity banDocument(BanParam banParam) {
        CloudDocument cloudDocument = CloudDocument.builder()
                .id(banParam.getFileId())
                .documentBan(1)
                .documentImg(CloudConstant.SYSTEM_BAN)
                .build();
        baseMapper.updateById(cloudDocument);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity unsealDocument(BanParam banParam) {
        CloudDocument document = baseMapper.selectById(banParam.getFileId());
        DocumentTypeEnum instance = DocumentTypeEnum.getInstance(document.getDocumentType());
        String img = "";
        if (Objects.nonNull(instance)) {
            img = instance.getImg();
        }

        CloudDocument cloudDocument = CloudDocument.builder()
                .id(banParam.getFileId())
                .documentBan(0)
                .documentImg(img)
                .build();
        baseMapper.updateById(cloudDocument);

        return new RespEntity(RespCode.SUCCESS);
    }

}
